﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Http;
using System.Net.Http.Headers;
using XclusiveClient.Classes;

namespace XclusiveClient
{
    public partial class EditAccount : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri("http://localhost:3676/");
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage response = client.GetAsync("api/Client/?company_name=" + Session["company_name"]).Result;
            if (response.IsSuccessStatusCode)
            {
                var c = response.Content.ReadAsAsync<Client>().Result;

                tbxusername.Text = c.username.ToString();
                tbxfull_name.Text = c.full_name.ToString();
                tbxpassword.Text = c.password.ToString();
                tbxemail.Text = c.email.ToString();
                tbxaddress.Text = c.address.ToString();
                tbxcontact_number.Text = c.contact_number.ToString();
                ddcompany_country.SelectedValue = c.company_country.ToString();
                tbxcompany_name.Text = c.company_name.ToString();
                tbxcompany_email.Text = c.company_email.ToString();
                tbxcompany_address.Text = c.company_address.ToString();
                Session["company_country"] = c.company_country.ToString();
            }
            else
            {
                lblStatus.Text = "Could not register for an account. Error code: " + response.StatusCode + ", Reason: " + response.ReasonPhrase.ToString() + "<br/>";
            }
        }

        protected void updateButton_Click(object sender, EventArgs e)
        {
            Client temp = new Client();
            temp.username = tbxusername.Text;
            temp.full_name = tbxfull_name.Text;
            temp.password = tbxpassword.Text;
            temp.email = tbxemail.Text;
            temp.address = tbxaddress.Text;
            temp.contact_number = Convert.ToInt32(tbxcontact_number.Text);
            temp.company_country = ddcompany_country.SelectedValue;
            temp.company_name = tbxcompany_name.Text;
            temp.company_email = tbxcompany_email.Text;
            temp.company_address = tbxcompany_address.Text;
            temp.account_type = "Administrative";

            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri("http://localhost:3676/");
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            var c = new Client()
            {
                username = temp.username,
                full_name = temp.full_name,
                password = temp.password,
                email = temp.email,
                address = temp.address,
                contact_number = temp.contact_number,
                company_country = temp.company_country,
                company_name = temp.company_name,
                company_email = temp.company_email,
                company_address = temp.company_address,
                account_type = temp.account_type,
            };

            HttpResponseMessage response = client.PostAsJsonAsync("api/Client/?company_name=" + Session["company_name"], c).Result;
            if (response.IsSuccessStatusCode)
            {
                Session["username"] = temp.username;
                Session["full_name"] = temp.full_name;
                Session["email"] = temp.email;
                Session["address"] = temp.address;
                Session["contact_number"] = temp.contact_number;
                Session["company_country"] = temp.company_country;
                Session["company_name"] = temp.company_name;
                Session["company_email"] = temp.company_email;
                Session["company_address"] = temp.company_address;
                lblStatus.Text = "Acount Information Updated.";
                Response.Redirect("/UpdatedSuccess.aspx");
            }
            else
            {
                lblStatus.Text = "Could not update your account. Error code: " + response.StatusCode + ", Reason: " + response.ReasonPhrase.ToString() + "<br/>";
            }
        }

        protected void btnCheck_Click(object sender, EventArgs e)
        {
            string country = Session["company_country"].ToString();

            HttpClient client = new HttpClient();
            String appID = "86f7a6c86cd241dd8577583283c2a392";
            client.BaseAddress = new Uri("http://openexchangerates.org/");
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage ratesResp = client.GetAsync("api/latest.json?appid=" + appID).Result;

            if (ratesResp.IsSuccessStatusCode)
            {
                var result = ratesResp.Content.ReadAsAsync<Rates>().Result;
                double rates = result.SGD; //Try to compare and choose accordingly to selection
                lblrates.Text += "<p>1 USD = " + rates + " " + country + "<p/>"; //display rate
            }
        }
    }
}